package com.hsl.test.dao.impl;

import com.hsl.test.aop.TAutowired;
import com.hsl.test.dao.AccountDao;
import com.hsl.test.pojo.AccountPO;
import com.hsl.test.utils.ConnectionUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author hsl
 * @Data 2021/12/14
 * @Desc
 */
public class AccountDaoImpl implements AccountDao {
    @TAutowired
    private ConnectionUtil connectionUtil;

    @Override
    public AccountPO queryAccountByCardNo(String cardNo) throws Exception {
        Connection conn = connectionUtil.getCurrentConn();
        String sql = "select * from user_account where cardNo=?";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1, cardNo);
        ResultSet resultSet = preparedStatement.executeQuery();
        AccountPO account = new AccountPO();
        while (resultSet.next()) {
            account.setCardNo(resultSet.getString("cardNo"));
            account.setName(resultSet.getString("name"));
            account.setMoney(resultSet.getInt("money"));
        }
        resultSet.close();
        preparedStatement.close();
        return account;
    }

    @Override
    public int updateAccountByCardNo(AccountPO account) throws Exception {
        Connection conn = connectionUtil.getCurrentConn();
        String sql = "update user_account set money=? where cardNo=?";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setInt(1, account.getMoney());
        preparedStatement.setString(2, account.getCardNo());
        int i = preparedStatement.executeUpdate();
        preparedStatement.close();
        return i;
    }
}
